//
//  StockTimeLineModel.m
//  K线图自治
//
//  Created by LK-IOS on 2017/12/11.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "StockTimeLineModel.h"
#import <CoreGraphics/CoreGraphics.h>


@interface StockTimeLineModel()


@end


@implementation StockTimeLineModel

{
    NSDictionary * _dict;
    NSNumber *preClosePrice;//前一分的价格
    NSNumber *lastPirce;//当前时间点的最新价
    NSNumber *priceChange;//当前时间点涨跌幅度
    NSNumber *AvgPrice;//前一天的收盘价
    NSNumber *totalVolume;//总的成交量
    NSNumber *CurrentVolume;//当前总的成交量
    NSNumber *TotalValue;//成交总金额
    NSNumber *MaxPrice;//价格最高值,当天能上涨的最高价格
    NSNumber *MinPrice;//价格最低值，当天能下跌的最低价格
    NSNumber *firstPrice;//当天第一个时间点的价格   ，暂时当做分时图的开盘价
    NSNumber *MaxUp;//最高涨幅
    NSNumber *MaxDown;//最高跌幅
    
    
    NSString *TimeDetail;//日期 包含年月日时分秒
    NSString *currentTime;//当前时间  时分
    NSString *minteTime; //当前显示的时分
    
    
    
    
}

-(NSNumber *)preClosePrice
{
    return _dict[@"closePrice"];
}
-(NSNumber *)lastPirce
{
    return _dict[@"openPrice"];
}
-(NSNumber *)priceChange
{
    NSString *last_px = [NSString stringWithFormat:@"%@" , _dict[@"openPrice"]];
    NSString *pre_close_px = [NSString stringWithFormat:@"%@" , _dict[@"closePrice"]];
    NSString *priceChangeString = [last_px pz_stringNumberBySubtracting:pre_close_px];
    return [NSNumber numberWithFloat:[priceChangeString floatValue]];
}
//平均价暂无 ，
-(NSNumber *)AvgPrice
{
    return [NSNumber numberWithFloat:100.0];
}
-(NSNumber *)totalVolume
{
    return _dict[@"volume"];
}
-(NSNumber *)currentVolume
{
    return _dict[@"nowVolume"];
}
//总成交额，暂无
-(NSNumber *)TotalValue
{
    return [NSNumber numberWithFloat:100.0];
}
-(NSNumber *)MaxPrice
{
    return MaxPrice;
}
-(NSNumber *)MinPrice
{
    return MinPrice;
}
-(NSNumber *)MaxUp
{
    return MaxUp;
}
-(NSNumber *)MaxDown
{
    return MaxDown;
}
-(NSNumber *)firstPrice
{
    return firstPrice;
}
//当前价格的涨跌幅度，为最新价减去上一次时间点的价 比上一次的价格
-(NSNumber *)currentUpDownRate
{
    NSString *rateString = [[NSString stringWithFormat:@"%@" , self.priceChange] pz_stringNumberByDividingBy:[NSString stringWithFormat:@"%@" , self.preClosePrice]];
    
    NSString *ratePer = [rateString pz_stringNumberByMultiplyingBy:@"100"];
    
    
    return [NSNumber numberWithFloat:[ratePer floatValue]];
}

-(NSString *)currentTime
{
    NSString *dateString = [NSString stringWithFormat:@"%@" , _dict[@"time"]];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [formate dateFromString:dateString];
    
    NSDateFormatter *formate1 = [[NSDateFormatter alloc] init];
    [formate1 setDateFormat:@"HH:mm"];
    
    return [formate1 stringFromDate:date];
}
-(NSString *)minteTime
{
//    if( [_dict[@"curr_time"] isEqualToString:@"11:30"] || [_dict[@"curr_time"] isEqualToString:@"13:00"]) {
//        return @"11:30/13:00";
//    }else{
//        return _dict[@"curr_time"];
//    }
    return _dict[@"time"];
    
}

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        _dict = dict;
        
        preClosePrice = _dict[@"closePrice"] ;
        lastPirce = _dict[@"openPrice"];
        AvgPrice = [NSNumber numberWithFloat:100.0];
        totalVolume = _dict[@"volume"];
        CurrentVolume = _dict[@"nowVolume"];
        TotalValue = [NSNumber numberWithFloat:100.0];
        currentTime = _dict[@"time"];
        priceChange = [NSNumber numberWithFloat:[[[NSString stringWithFormat:@"%@" , _dict[@"closePrice"]] pz_stringNumberBySubtracting:[NSString stringWithFormat:@"%@" ,_dict[@"openPrice"]]] floatValue]];
        
    }
    return self;
}


-(void)upDateTimeLineLimitDataWithDataArray:(NSArray *)dataArray Index:(NSInteger)index
{
    if (!dataArray.count)return;
    NSDictionary *dict = dataArray[0];
    firstPrice = dict[@"openPrice"];
    CGFloat max1 = [[[dataArray valueForKeyPath:@"openPrice"] valueForKeyPath:@"@max.floatValue"] floatValue];
    CGFloat min1 = [[[dataArray valueForKeyPath:@"openPrice"] valueForKeyPath:@"@min.floatValue"] floatValue];
    CGFloat open1 = [firstPrice floatValue];
    if (max1 == min1 || open1 <= 0) {//保证最高最低价格相同时，程序不出错
        MaxDown = [NSNumber numberWithFloat:-10.02];
        MaxUp = [NSNumber numberWithFloat:10.02];
        NSDictionary *dict = dataArray[0];
        firstPrice = dict[@"openPrice"];
        NSString *maxPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"1.1002"];
        MaxPrice = [NSNumber numberWithFloat:[maxPriceString floatValue]];

        NSString *minPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"0.8998"];
        MinPrice = [NSNumber numberWithFloat:[minPriceString floatValue]];

    }else{

        if (dataArray.count < 5) {//分时数据小于5的时候，按照涨跌10.02计算
            MaxDown = [NSNumber numberWithFloat:-10.02];
            MaxUp = [NSNumber numberWithFloat:10.02];
            //第一个时间点的价格
            NSDictionary *dict = dataArray[0];
            firstPrice = dict[@"openPrice"];
            //计算最高价格
            NSString *maxPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"1.1002"];
            MaxPrice = [NSNumber numberWithFloat:[maxPriceString floatValue]];

            NSString *minPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"0.8998"];
            MinPrice = [NSNumber numberWithFloat:[minPriceString floatValue]];

        }else{//分时数据在五个以上的时候，另行计算涨跌幅度

            NSDictionary *dict = dataArray[0];
            firstPrice = [NSNumber numberWithFloat:[dict[@"openPrice"] floatValue]];

            CGFloat max = [[[dataArray valueForKeyPath:@"openPrice"] valueForKeyPath:@"@max.floatValue"] floatValue];
            CGFloat min = [[[dataArray valueForKeyPath:@"openPrice"] valueForKeyPath:@"@min.floatValue"] floatValue];
            CGFloat open = [firstPrice floatValue];

            CGFloat maxUp = fabs(max - open);
            CGFloat minDown = fabs(min - open);

            CGFloat maxCha = maxUp > minDown ? maxUp : minDown;
            CGFloat maxUpDownFloat = maxCha / [firstPrice floatValue];
            NSString *maxUpDownString = [NSString stringWithFormat:@"%.2f" , maxUpDownFloat * 100.0];//变化百分比字符串
            
            
            MaxDown = [NSNumber numberWithFloat:[[maxUpDownString pz_stringNumberByMultiplyingBy:@"1"] floatValue]];
            MaxUp = MaxDown;
            MaxPrice = [NSNumber numberWithFloat:[[[NSString stringWithFormat:@"%f" , maxUpDownFloat + 1] pz_stringNumberByMultiplyingBy:[NSString stringWithFormat:@"%@" , firstPrice]] floatValue]];
            MinPrice = [NSNumber numberWithFloat:[[[NSString stringWithFormat:@"%f" , 1 - maxUpDownFloat] pz_stringNumberByMultiplyingBy:[NSString stringWithFormat:@"%@" , firstPrice]] floatValue]];
            
            //最大最小价格和开盘价比较相同时，按涨跌百分之十计算
            if (MaxPrice.floatValue <= firstPrice.floatValue || MinPrice.floatValue >= firstPrice.floatValue) {
                MaxDown = [NSNumber numberWithFloat:-10.02];
                MaxUp = [NSNumber numberWithFloat:10.02];
                //第一个时间点的价格
                NSDictionary *dict = dataArray[0];
                firstPrice = dict[@"openPrice"];
                //计算最高价格
                NSString *maxPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"1.1002"];
                MaxPrice = [NSNumber numberWithFloat:[maxPriceString floatValue]];
                
                NSString *minPriceString = [[NSString stringWithFormat:@"%@" , firstPrice] pz_stringNumberByMultiplyingBy:@"0.8998"];
                MinPrice = [NSNumber numberWithFloat:[minPriceString floatValue]];
            }

        }

    }

}


@end
